<?php
/**
 * Created by PhpStorm.
 * User: frankpian
 * Date: 17-12-5
 * Time: 上午10:55
 */

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Regex as RegexValidator;
use Phalcon\Validation\Validator\Between;
use Phalcon\Validation\Validator\StringLength;

class BugController extends CloudControllerBase
{

    /******************************    记载日志管理    ***********************************************************/
    public function planeBugListAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT * FROM app_bug)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "version" => "管家版本号",
                    "system" => "操作系统",
                    "level" => "错误级别",
                    "data" => "客户问题反馈",
                    "type" => "应用类型",
                    "log_url" => "飞行日志下载",
                    "created_at" => "创建时间",
                ), $table
                    , array(
                        ["id", "="],
                        ["version", "like", "%", "%"],
                        ["system", "like", "%", "%"],
                        ["level", "="],
                        ["data", "like", "%", "%"],
                        ["type", "="],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ),"created_at desc","id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()

            )

        );
    }

    /******************************    管家意见反馈    ***********************************************************/
    //官网反馈意见接口
    public function submitSuggestionAction()
    {
        header('Content-type: application/json');
        header('Access-Control-Allow-Origin:*');
        if ($this->redis->get("submitSuggest") == true){
            $this->returnError("提交过于频繁，请1分钟后提交");
        }

        $this->checkRequestParams(array(
            array(
                "key" => "type",
                "type" => "presence",
                "message" => "没有选择分类"),
            array(
                "key" => "description",
                "type" => "presence",
                "message" => "请填写内容"),
        ));
        $imgaesListStr = $this->getPost("images");
        $this->insertSqlData(array(
            "table" => "suggestion",
            "fieldList" => array("type", "description", "firmware_type_id", "phone", "email"),
            "addItems" => array("images" => $imgaesListStr)
        ));
        $this->redis->setex("submitSuggest", 60, true);
        $this->returnSuccess("提交成功，感谢您的留言！");


    }

    public function managerSuggestionListAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT *, suggestion.type AS sugtype FROM suggestion)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "sugtype" => "意见类型",
                    "description" => "意见详情",
//                    "images" => "上传图片",
                    "phone" => "电话",
                    "email" => "Email",
                    "created_at" => "创建时间",
                ), $table
                    , array(
                        ["id", "="],
                        ["sugtype", "="],
                        ["description", "like", "%", "%"],
                        ["phone", "like", "%", "%"],
                        ["email", "like", "%", "%"],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ),"created_at desc","id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()

            )

        );
    }

    public function detailManagerSuggestionAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->transferErrorPage("权限不足", "");
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case $this::REQUEST_ACTION_DETAIL: {
                $this->checkRequestParams(array(
                    array(
                        "key" => "id",
                        "type" => "presence",
                        "message" => "请求参数错误"),
                ));
                $keyId = $this->getPost("id");
                $sql = "SELECT * FROM suggestion WHERE id=$keyId";
                $keyData = $this->fetchOne($sql);
                $this->returnJson($keyData);
            };
                break;
            case $this::REQUEST_ACTION_UPDATE: {

            };
                break;
        }
    }

    /******************************    crash报告    ***********************************************************/
    public function reportCrashAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT * FROM crash_report)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "name" => "应用名称",
                    "version" => "版本号",
                    "url" => "错误报告",
                    "created_at" => "上传时间",
                ), $table
                    , array(
                        ["id", "="],
                        ["name", "like", "%", "%"],
                        ["version", "like", "%", "%"],
                        ["phone", "like", "%", "%"],
                        ["email", "like", "%", "%"],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ),"created_at desc","id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()

            )

        );
    }

    public function filedItemChanged($table, $name, $value)
    {
        switch($name){
            case "level": {
                switch($value){
                    case "0":
                        return "message";
                        break;
                    case "1":
                        return "debug";
                        break;
                    case "2":
                        return "info";
                        break;
                    case "3":
                        return "warning";
                        break;
                    case "4":
                        return "error";
                        break;
                }
            };
                break;
            case "type": {
                switch($value){
                    case "0":
                        return "windows客户端";
                        break;
                    case "1":
                        return "iOS客户端";
                        break;
                    case "2":
                        return "android客户端";
                        break;
                    case "3":
                        return "飞控与电台固件";
                        break;
                }
            };
                break;
            case "sugtype": {
                switch($value){
                    case "0":
                        return "建议";
                        break;
                    case "1":
                        return "咨询问题";
                        break;
                    case "2":
                        return "投诉";
                        break;
                    case "3":
                        return "bug反馈";
                        break;
                }
            };
                break;
            case "url":
            case "log_url": {
                if($value){
                    return "<a href='" . $value . "'>下载文件</a>";
                }
            };
                break;
        }
        return $value;
    }

}